#include "page.h"
#include "ssd1327.h"
#include "ec11.h"
#include "vl53l0x.h"
#include "encode.h"
#include "adc128s.h"
#include "imu.h"
#include "buzz.h"
#include "control.h"
#include "element.h"
#include "motor.h"

#include "zf_delay.h"
#include <stdio.h>

Page_type xdata Page;

void Page_RawInd(void)
{
    sprintf(Page.Buff, "RawIndVal %d/%d", RawInd + 1, EndPage);
    u8g2_DrawStr(&u8g2, 0, 8, Page.Buff);

    sprintf(Page.Buff, "S6:%d", ADC.Raw[7]);
    u8g2_DrawStr(&u8g2, 0, 16, Page.Buff);
    sprintf(Page.Buff, "S0:%d", ADC.Raw[1]);
    u8g2_DrawStr(&u8g2, 64, 16, Page.Buff);

    sprintf(Page.Buff, "S5:%d", ADC.Raw[6]);
    u8g2_DrawStr(&u8g2, 0, 24, Page.Buff);
    sprintf(Page.Buff, "S1:%d", ADC.Raw[2]);
    u8g2_DrawStr(&u8g2, 64, 24, Page.Buff);

    sprintf(Page.Buff, "S4:%d", ADC.Raw[5]);
    u8g2_DrawStr(&u8g2, 0, 32, Page.Buff);
    sprintf(Page.Buff, "S2:%d", ADC.Raw[3]);
    u8g2_DrawStr(&u8g2, 64, 32, Page.Buff);

    sprintf(Page.Buff, "S3:%d", ADC.Raw[4]);
    u8g2_DrawStr(&u8g2, 36, 40, Page.Buff);

    sprintf(Page.Buff, "TMain:%ld", Counter.Main);
    u8g2_DrawStr(&u8g2, 0, 48, Page.Buff);

    sprintf(Page.Buff, "Tim4:%ld", Counter.Tim4);
    u8g2_DrawStr(&u8g2, 0, 56, Page.Buff);

    sprintf(Page.Buff, "Velo:%d", CarVelocity);
    u8g2_DrawStr(&u8g2, 0, 64, Page.Buff);

    sprintf(Page.Buff, "Disp:%ld", Enco.Displace);
    u8g2_DrawStr(&u8g2, 0, 72, Page.Buff);

    sprintf(Page.Buff, "Temp:%2.2fc", IMU.Temp);
    u8g2_DrawStr(&u8g2, 0, 80, Page.Buff);

    sprintf(Page.Buff, "Inf:%2.2fcm", Elem.Distance);
    u8g2_DrawStr(&u8g2, 0, 88, Page.Buff);

    sprintf(Page.Buff, "Bat:%2.1fV", ADC.BatVol);
    u8g2_DrawStr(&u8g2, 0, 96, Page.Buff);

    sprintf(Page.Buff, "ec11:%d", EC11.encoRealVal);
    u8g2_DrawStr(&u8g2, 0, 104, Page.Buff);

    sprintf(Page.Buff, "IndErr:%f", Ctrl.IndErr);
    u8g2_DrawStr(&u8g2, 0, 128, Page.Buff);

    u8g2_SendBuffer(&u8g2);

    switch (EC11.btnStatus)
    {
    case BUTTON_DOUBLE:
        Ctrl_CarStart(); // 发车
        break;
    }
}

void Page_StdInd(void)
{
    sprintf(Page.Buff, "StdIndVal %d/%d", StdInd + 1, EndPage);
    u8g2_DrawStr(&u8g2, 0, 8, Page.Buff);

    sprintf(Page.Buff, "S6:%d", ADC.Std[6]);
    u8g2_DrawStr(&u8g2, 0, 16, Page.Buff);
    sprintf(Page.Buff, "S0:%d", ADC.Std[0]);
    u8g2_DrawStr(&u8g2, 64, 16, Page.Buff);

    sprintf(Page.Buff, "S5:%d", ADC.Std[5]);
    u8g2_DrawStr(&u8g2, 0, 24, Page.Buff);
    sprintf(Page.Buff, "S1:%d", ADC.Std[1]);
    u8g2_DrawStr(&u8g2, 64, 24, Page.Buff);

    sprintf(Page.Buff, "S4:%d", ADC.Std[4]);
    u8g2_DrawStr(&u8g2, 0, 32, Page.Buff);
    sprintf(Page.Buff, "S2:%d", ADC.Std[2]);
    u8g2_DrawStr(&u8g2, 64, 32, Page.Buff);

    sprintf(Page.Buff, "S3:%d", ADC.Std[3]);
    u8g2_DrawStr(&u8g2, 36, 40, Page.Buff);

    sprintf(Page.Buff, "Mi6:%d", ADC.Min[6]);
    u8g2_DrawStr(&u8g2, 0, 48, Page.Buff);
    sprintf(Page.Buff, "Mi0:%d", ADC.Min[0]);
    u8g2_DrawStr(&u8g2, 64, 48, Page.Buff);

    sprintf(Page.Buff, "Mi5:%d", ADC.Min[5]);
    u8g2_DrawStr(&u8g2, 0, 56, Page.Buff);
    sprintf(Page.Buff, "Mi1:%d", ADC.Min[1]);
    u8g2_DrawStr(&u8g2, 64, 56, Page.Buff);

    sprintf(Page.Buff, "Mi4:%d", ADC.Min[4]);
    u8g2_DrawStr(&u8g2, 0, 64, Page.Buff);
    sprintf(Page.Buff, "Mi2:%d", ADC.Min[2]);
    u8g2_DrawStr(&u8g2, 64, 64, Page.Buff);

    sprintf(Page.Buff, "Mi3:%d", ADC.Min[3]);
    u8g2_DrawStr(&u8g2, 36, 72, Page.Buff);

    sprintf(Page.Buff, "Ma6:%d", ADC.Max[6]);
    u8g2_DrawStr(&u8g2, 0, 80, Page.Buff);
    sprintf(Page.Buff, "Ma0:%d", ADC.Max[0]);
    u8g2_DrawStr(&u8g2, 64, 80, Page.Buff);

    sprintf(Page.Buff, "Ma5:%d", ADC.Max[5]);
    u8g2_DrawStr(&u8g2, 0, 88, Page.Buff);
    sprintf(Page.Buff, "Ma1:%d", ADC.Max[1]);
    u8g2_DrawStr(&u8g2, 64, 88, Page.Buff);

    sprintf(Page.Buff, "Ma4:%d", ADC.Max[4]);
    u8g2_DrawStr(&u8g2, 0, 96, Page.Buff);
    sprintf(Page.Buff, "Ma2:%d", ADC.Max[2]);
    u8g2_DrawStr(&u8g2, 64, 96, Page.Buff);

    sprintf(Page.Buff, "Ma3:%d", ADC.Max[3]);
    u8g2_DrawStr(&u8g2, 36, 104, Page.Buff);

    u8g2_SendBuffer(&u8g2);

    switch (EC11.btnStatus)
    {
    case BUTTON_CLICKED:
        break;
    case BUTTON_DOUBLE:
        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "Start");
        u8g2_SendBuffer(&u8g2);

        ADC_RoadStd();

        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "Finish");
        u8g2_SendBuffer(&u8g2);
        break;
    }
}

void Page_RoundInInd(void)
{
    sprintf(Page.Buff, "RoundInVal %d/%d", RoundInInd + 1, EndPage);
    u8g2_DrawStr(&u8g2, 0, 8, Page.Buff);

    sprintf(Page.Buff, "6i:%d", Elem.RoundInMin[6]);
    u8g2_DrawStr(&u8g2, 0, 16, Page.Buff);
    sprintf(Page.Buff, "0i:%d", Elem.RoundInMin[0]);
    u8g2_DrawStr(&u8g2, 64, 16, Page.Buff);

    sprintf(Page.Buff, "5i:%d", Elem.RoundInMin[5]);
    u8g2_DrawStr(&u8g2, 0, 24, Page.Buff);
    sprintf(Page.Buff, "1i:%d", Elem.RoundInMin[1]);
    u8g2_DrawStr(&u8g2, 64, 24, Page.Buff);

    sprintf(Page.Buff, "4i:%d", Elem.RoundInMin[4]);
    u8g2_DrawStr(&u8g2, 0, 32, Page.Buff);
    sprintf(Page.Buff, "2i:%d", Elem.RoundInMin[2]);
    u8g2_DrawStr(&u8g2, 64, 32, Page.Buff);

    sprintf(Page.Buff, "3i:%d", Elem.RoundInMin[3]);
    u8g2_DrawStr(&u8g2, 36, 40, Page.Buff);

    sprintf(Page.Buff, "6a:%d", Elem.RoundInMax[6]);
    u8g2_DrawStr(&u8g2, 0, 48, Page.Buff);
    sprintf(Page.Buff, "0a%d", Elem.RoundInMax[0]);
    u8g2_DrawStr(&u8g2, 64, 48, Page.Buff);

    sprintf(Page.Buff, "5a:%d", Elem.RoundInMax[5]);
    u8g2_DrawStr(&u8g2, 0, 56, Page.Buff);
    sprintf(Page.Buff, "1a:%d", Elem.RoundInMax[1]);
    u8g2_DrawStr(&u8g2, 64, 56, Page.Buff);

    sprintf(Page.Buff, "4a:%d", Elem.RoundInMax[4]);
    u8g2_DrawStr(&u8g2, 0, 64, Page.Buff);
    sprintf(Page.Buff, "2a:%d", Elem.RoundInMax[2]);
    u8g2_DrawStr(&u8g2, 64, 64, Page.Buff);

    sprintf(Page.Buff, "3a:%d", Elem.RoundInMax[3]);
    u8g2_DrawStr(&u8g2, 36, 72, Page.Buff);

    u8g2_SendBuffer(&u8g2);

    switch (EC11.btnStatus)
    {
    case BUTTON_CLICKED:
        break;
    case BUTTON_DOUBLE:
        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "Start");
        u8g2_SendBuffer(&u8g2);

        RoundIn_ScanPoint();

        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "Finish");
        u8g2_SendBuffer(&u8g2);
        break;
    }
}

void Page_RoundOutInd(void)
{
    sprintf(Page.Buff, "RoundOutVal %d/%d", RoundOutInd + 1, EndPage);
    u8g2_DrawStr(&u8g2, 0, 8, Page.Buff);

    sprintf(Page.Buff, "6i:%d", Elem.RoundOutMin[6]);
    u8g2_DrawStr(&u8g2, 0, 16, Page.Buff);
    sprintf(Page.Buff, "0i:%d", Elem.RoundOutMin[0]);
    u8g2_DrawStr(&u8g2, 64, 16, Page.Buff);

    sprintf(Page.Buff, "5i:%d", Elem.RoundOutMin[5]);
    u8g2_DrawStr(&u8g2, 0, 24, Page.Buff);
    sprintf(Page.Buff, "1i:%d", Elem.RoundOutMin[1]);
    u8g2_DrawStr(&u8g2, 64, 24, Page.Buff);

    sprintf(Page.Buff, "4i:%d", Elem.RoundOutMin[4]);
    u8g2_DrawStr(&u8g2, 0, 32, Page.Buff);
    sprintf(Page.Buff, "2i%d", Elem.RoundOutMin[2]);
    u8g2_DrawStr(&u8g2, 64, 32, Page.Buff);

    sprintf(Page.Buff, "3i:%d", Elem.RoundOutMin[3]);
    u8g2_DrawStr(&u8g2, 36, 40, Page.Buff);

    sprintf(Page.Buff, "6a:%d", Elem.RoundOutMax[6]);
    u8g2_DrawStr(&u8g2, 0, 48, Page.Buff);
    sprintf(Page.Buff, "0a:%d", Elem.RoundOutMax[0]);
    u8g2_DrawStr(&u8g2, 64, 48, Page.Buff);

    sprintf(Page.Buff, "5a:%d", Elem.RoundOutMax[5]);
    u8g2_DrawStr(&u8g2, 0, 56, Page.Buff);
    sprintf(Page.Buff, "1a:%d", Elem.RoundOutMax[1]);
    u8g2_DrawStr(&u8g2, 64, 56, Page.Buff);

    sprintf(Page.Buff, "4a:%d", Elem.RoundOutMax[4]);
    u8g2_DrawStr(&u8g2, 0, 64, Page.Buff);
    sprintf(Page.Buff, "2a:%d", Elem.RoundOutMax[2]);
    u8g2_DrawStr(&u8g2, 64, 64, Page.Buff);

    sprintf(Page.Buff, "3a:%d", Elem.RoundOutMax[3]);
    u8g2_DrawStr(&u8g2, 36, 72, Page.Buff);

    u8g2_SendBuffer(&u8g2);

    switch (EC11.btnStatus)
    {
    case BUTTON_CLICKED:
        break;
    case BUTTON_DOUBLE:
        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "Start");
        u8g2_SendBuffer(&u8g2);

        RoundOut_ScanPoint();

        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "Finish");
        u8g2_SendBuffer(&u8g2);
        break;
    }
}

void Page_IMUOffset(void)
{
    sprintf(Page.Buff, "IMUCaliVal %d/%d", IMUOffset + 1, EndPage);
    u8g2_DrawStr(&u8g2, 0, 8, Page.Buff);

    sprintf(Page.Buff, "Gx:%f", IMU.GyroOffset[x]);
    u8g2_DrawStr(&u8g2, 0, 16, Page.Buff);

    sprintf(Page.Buff, "Gy:%f", IMU.GyroOffset[y]);
    u8g2_DrawStr(&u8g2, 0, 24, Page.Buff);

    sprintf(Page.Buff, "Gz:%f", IMU.GyroOffset[z]);
    u8g2_DrawStr(&u8g2, 0, 32, Page.Buff);

    sprintf(Page.Buff, "Yo:%f", IMU.AngleOffset[yaw]);
    u8g2_DrawStr(&u8g2, 0, 40, Page.Buff);

    sprintf(Page.Buff, "Po:%f", IMU.AngleOffset[pitch]);
    u8g2_DrawStr(&u8g2, 0, 48, Page.Buff);

    sprintf(Page.Buff, "Ro:%f", IMU.AngleOffset[roll]);
    u8g2_DrawStr(&u8g2, 0, 56, Page.Buff);

    sprintf(Page.Buff, "Yo:%f", IMU.EulerAngle[yaw]);
    u8g2_DrawStr(&u8g2, 0, 64, Page.Buff);

    sprintf(Page.Buff, "Po:%f", IMU.EulerAngle[pitch]);
    u8g2_DrawStr(&u8g2, 0, 72, Page.Buff);

    sprintf(Page.Buff, "Ro:%f", IMU.EulerAngle[roll]);
    u8g2_DrawStr(&u8g2, 0, 80, Page.Buff);

    u8g2_SendBuffer(&u8g2);

    switch (EC11.btnStatus)
    {
    case BUTTON_CLICKED:
        break;
    case BUTTON_DOUBLE:
        IMU.GyroOffset[x] = 0;
        IMU.GyroOffset[y] = 0;
        IMU.GyroOffset[z] = 0;
        IMU.AngleOffset[yaw] = 0;
        IMU.AngleOffset[pitch] = 0;
        IMU.AngleOffset[roll] = 0;
        IMU.CaliGyro = ENABLE;
        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "Start");
        u8g2_SendBuffer(&u8g2);
        delay_ms(1000);
        break;
    }
}

void Page_ElemSet()
{
    sprintf(Page.Buff, "CtrlSet %d/%d", ElemSet + 1, EndPage);
    u8g2_DrawStr(&u8g2, 0, 8, Page.Buff);

    u8g2_DrawStr(&u8g2, 16, 62, "CtrlSettings");
    u8g2_DrawStr(&u8g2, 0, 70, "Double to switch");
    u8g2_SendBuffer(&u8g2);

    switch (EC11.btnStatus)
    {
    case BUTTON_DOUBLE:
    //进入菜单会直接读取值，退出时要手动保存一下
        Elem.FlashRead = TRUE;
        Elem_E2PROM();
        Page.ElemSet = ENABLE;
        break;
    case BUTTON_LONG:
    //长按把原始值赋值给FLASH
        Elem.FlashWrite = TRUE;
        Elem_E2PROM();
        break;
    }
}

void Page_ButtonTest()
{
    u8g2_ClearDisplay(&u8g2);
    switch (EC11.btnStatus)
    {
    case BUTTON_CLICKED:
        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "CLICKED");
        u8g2_SendBuffer(&u8g2);
        break;
    case BUTTON_DOUBLE:
        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "DOUBLE");
        u8g2_SendBuffer(&u8g2);
        break;
    case BUTTON_LONG:
        u8g2_ClearBuffer(&u8g2);
        u8g2_DrawStr(&u8g2, 48, 68, "LONG");
        u8g2_SendBuffer(&u8g2);
        break;
    }
}

void Page_Switch(void)
{
    if (EC11.encoRealVal >= 3)
    {
        EC11.encoRealVal = 0;
        Page.Num++;
        if (Page.Num >= EndPage)
        {
            Page.Num = 0;
        }
    }
    else if (EC11.encoRealVal <= -3)
    {
        EC11.encoRealVal = 0;
        Page.Num--;
        if (Page.Num < 0)
        {
            Page.Num = EndPage - 1;
        }
    }
}

void Page_Process(void)
{
    u8g2_ClearBuffer(&u8g2);
    Page_Switch();

    switch (Page.Num)
    {
    case RawInd:
        Page_RawInd();
        break;
    case StdInd:
        Page_StdInd();
        break;
    case RoundInInd:
        Page_RoundInInd();
        break;
    case RoundOutInd:
        Page_RoundOutInd();
        break;
    case IMUOffset:
        Page_IMUOffset();
        break;
    case ElemSet:
        Page_ElemSet();
        break;
    case ButtonTest:
        Page_ButtonTest();
        break;
    }
    EC11.btnStatus = BUTTON_NONE;
}

void Page_Init()
{
    Page.Num = RawInd;
    Page.ElemSet = DISABLE;
}